package com.timoon.app.security;

import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;

import com.timoon.sample.dao.RoleDAO;
import com.timoon.sample.dao.TUserDAO;
import com.timoon.sample.dao.UserRoleDAO;
import com.timoon.sample.dao.model.Role;
import com.timoon.sample.dao.model.RoleExample;
import com.timoon.sample.dao.model.TUser;
import com.timoon.sample.dao.model.TUserExample;
import com.timoon.sample.dao.model.UserRole;
import com.timoon.sample.dao.model.UserRoleExample;
/**
 *<p></p>
 *<p></p>
 *<p>Copyright:Copyright(c)2013.4.1</p>
 *<p>Company:timoon</p>
 *@author 付乐天
 *@version 1.0 */
public class BaseUserDetailsService implements UserDetailsService ,ApplicationContextAware {

	private TUserDAO tUserDAO;
	private UserRoleDAO userRoleDAO;
	private RoleDAO roleDAO;
	
	@SuppressWarnings("unchecked")
	@Override
	public UserDetails loadUserByUsername(final String arg0)
			throws UsernameNotFoundException {
		// TODO Auto-generated method stub
		List<TUser> tusers=this.tUserDAO.selectByExample(new TUserExample(){{
			this.createCriteria().andAccountEqualTo(arg0);
		}});
		
		TUser user=tusers.get(0);
		
		if (user == null)
			throw new UsernameNotFoundException("User not found: " + arg0);
		else {
			return makeAcegiUser(user);
		}
	}

	private org.springframework.security.core.userdetails.User makeAcegiUser(TUser tuser) {
		Collection<GrantedAuthority> grantedAuths = obtionGrantedAuthorities(tuser); 
		return new org.springframework.security.core.userdetails.User(tuser.getAccount(), tuser.getPassword(), true, true, true, true, grantedAuths);
	}
	
	private Set<GrantedAuthority> obtionGrantedAuthorities(final TUser tuser) 
	{  
		
		if(tuser.getAccount().equals("admin")){
			
	        Set<GrantedAuthority> authSet = new HashSet<GrantedAuthority>(); 
	        
	        List<Role> allRole=this.roleDAO.selectByExample(new RoleExample());
			for (Role role:allRole) {
				String roleStr = "ROLE_" + role.getId();
				SimpleGrantedAuthority simpleGrantedAuthority = new SimpleGrantedAuthority(roleStr);
				authSet.add(simpleGrantedAuthority);
			}
			String roleStr = "ROLE_USER";
			SimpleGrantedAuthority simpleGrantedAuthority = new SimpleGrantedAuthority(roleStr);
			authSet.add(simpleGrantedAuthority);
			return authSet; 
		}else{
			@SuppressWarnings("unchecked")
			List<UserRole> userRoles=this.userRoleDAO.selectByExample(new UserRoleExample(){
				{
					this.createCriteria().andUserIdEqualTo(tuser.getId());
				}
			});
	        Set<GrantedAuthority> authSet = new HashSet<GrantedAuthority>(); 
			for (UserRole userRole:userRoles) {
				Role role=this.roleDAO.selectByPrimaryKey(userRole.getRoleId());
				String roleStr = "ROLE_" + role.getId();
				SimpleGrantedAuthority simpleGrantedAuthority = new SimpleGrantedAuthority(roleStr);
				authSet.add(simpleGrantedAuthority);
			}
			String roleStr = "ROLE_USER";
			SimpleGrantedAuthority simpleGrantedAuthority = new SimpleGrantedAuthority(roleStr);
			authSet.add(simpleGrantedAuthority);
			return authSet; 
		}
    }
	
	@Override
	public void setApplicationContext(ApplicationContext context)
			throws BeansException {
		// TODO Auto-generated method stub
		this.userRoleDAO=(UserRoleDAO) context.getBean("userRoleDAOImpl");
		this.tUserDAO=(TUserDAO) context.getBean("tUserDAOImpl");
		this.roleDAO=(RoleDAO) context.getBean("roleDAOImpl");
	}

}
